package com.intel.surface;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class IpSetDlg extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textField;

    public IpSetDlg(String ip) {
        setBounds(100, 100, 380, 212);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JLabel lblNewLabel = new JLabel("服务器地址及端口");
            lblNewLabel.setFont(new Font("宋体", Font.BOLD, 12));
            lblNewLabel.setBounds(27, 54, 120, 25);
            contentPanel.add(lblNewLabel);
        }

        textField = new JTextField(ip);
        textField.setBounds(180, 57, 150, 22);
        contentPanel.add(textField);
        textField.setColumns(10);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("确定");
                buttonPane.add(okButton);
                okButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String[] strs = textField.getText().split(":");
                        if (strs.length != 2) {
                            JOptionPane.showMessageDialog(IpSetDlg.this, "请输入正确的IP和端口");
                            return;
                        }

                        SettingConfig.getInstance().setServerAddress(strs[0]);
                        SettingConfig.getInstance().setServerPort(strs[1]);
                        IpSetDlg.this.dispose();
                    }
                });
            }
            {
                JButton cancelButton = new JButton("取消");
                buttonPane.add(cancelButton);
                cancelButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        IpSetDlg.this.dispose();
                    }
                });
            }
        }
    }
}
